<?php
/**
 * Copyright 2013 Marek Kaszewski
 *
 * This file is part of the JHUB1 Online Community Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace Jhub1_Console\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Session\Container;
use Zend\Console\Request as ConsoleRequest;
use Application\Entity\BO\UsersBO;
use Application\Entity\BO\AgentsBO;
use Application\Entity\BO\EndpointValuesBO;

class UsersController extends AbstractActionController
{
    public function createAction()
    {
        $request = $this->getRequest();
        if (!$request instanceof ConsoleRequest){
            throw new \RuntimeException('You can only use this action from a console!');
        }
        $isVerbose = $request->getParam('verbose',false) || $request->getParam('v',false);
        $userEmail = $request->getParam('userEmail');
        $password  = $request->getParam('password');
        $operation = $request->getParam('operation');
        $userBO = new UsersBO($this->getServiceLocator());
        /**
         * @TODO Email validation
         */
        $formData['email'] = $userEmail;
        switch ($operation) {
            case 'create':
                if ($password != '' && $password != null){
                    $formData['password'] = $password;
                } else {
                    $formData['password'] = "1qaz2wsx";
                }
                $formData['status'] = 1;
                $existUserPO = $userBO->getUserByEmail($formData['email']);
                if (null == $existUserPO) {
                    $newUserPO = $userBO->insertUser($formData);
                    if($isVerbose) {
                        echo 'New user: ' . $formData['email'] . ' added.' . PHP_EOL;
                    }
                } else {
                    if($isVerbose) {
                        echo 'User: ' . $formData['email'] . ' already exists in database.' . PHP_EOL;
                    }
                }
                exit;
            case 'status':
                $existUserPO = $userBO->getUserByEmail($formData['email']);
                echo $existUserPO->toString();
                exit;
            case 'activate':

                exit;
            case 'deactivate':

                exit;
            case 'delete':
/*                $ag = new AgentsBO($this->getServiceLocator());

                $agents = array('tmgguhxrv6','r6r7a42ng8');
                $epv = new EndpointValuesBO($this->getServiceLocator());
                $res = $epv->checkForNewEndpoints($agents);
                var_dump($res);*/
                exit;

        }
    }
}